<script setup lang="ts">
import { reactive, ref, onMounted } from "vue";
import { useMenu } from "./utils/hook";
import { transformI18n } from "@/plugins/i18n";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import organTree from "@/components/organTree.vue";
import productTree from "@/components/productTree.vue";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "powerList"
});
let treeInfoRef = ref(null);
let treeInfoRef2 = ref(null);
const formRef = ref();
const tableRef = ref();
let {
  saveHandle,
  selectPag,
  dataChange,
  pagination,
  eidtformRef,
  editform,
  closeDialog,
  formVisible,
  roleOptions3,
  roleOptions,
  roleOptions2,
  onChange,
  loading2,
  columns,
  dataList,
  onSearch,
  resetForm,
  openDialog,
  handleDelete,
  handleSelectionChange,
  handleSizeChange,
  handleCurrentChange
} = useMenu();
//选择机构
const selectHandle = () => {
  treeInfoRef.value.getgetComanyTreeInfo();
  treeInfoRef.value.dialogTree = true;
};
//机构
const cropper = data => {
  editform.value.permitCompanyName = data.checName.join(",");
  editform.value.permitCompanyCode = data.checKay.join(",");
};
//选择产品
const selectHandle2 = () => {
  treeInfoRef2.value.dialogTree = true;
};
//产品
const cropper2 = data => {
  editform.value.permitProductName = data.checName;
  editform.value.permitProductCode = data.checKay;
};
function onFullscreen() {
  // 重置表格高度
  tableRef.value.setAdaptive();
}
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="机构：" prop="comCode">
        <el-select
          v-model="selectPag.comCode"
          placeholder="请选择"
          clearable
          class="!w-[180px]"
        >
          <el-option
            v-for="(item, index) in roleOptions"
            :key="index"
            :value="item.comCode"
            :label="item.comCname"
          >
            {{ item.comCname }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工：" prop="userCode">
        <el-select
          v-model="selectPag.userCode"
          placeholder="请选择"
          clearable
          class="!w-[180px]"
        >
          <el-option
            v-for="(item, index) in roleOptions2"
            :key="index"
            :value="item.codeCode"
            :label="item.codeName"
          >
            {{ item.codeName }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="岗位：" prop="gradeId">
        <el-select
          v-model="selectPag.gradeId"
          placeholder="请选择"
          clearable
          class="!w-[180px]"
        >
          <el-option
            v-for="(item, index) in roleOptions3"
            :key="index"
            :value="item.codeCode"
            :label="item.codeName"
          >
            {{ item.codeName }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态：" prop="validStatus">
        <el-select
          v-model="selectPag.validStatus"
          placeholder="请选择"
          clearable
          class="!w-[180px]"
        >
          <el-option label="申请中" value="0" />
          <el-option label="已入职" value="1" />
          <el-option label="离职" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="入职时间：" prop="value1">
        <el-date-picker
          v-model="selectPag.value1"
          type="datetimerange"
          range-separator="到"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          @change="dataChange"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading2"
          @click="onSearch"
        >
          搜索
        </el-button>
        <!-- <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button> -->
      </el-form-item>
    </el-form>

    <PureTableBar
      title="员工列表"
      :columns="columns"
      :isExpandAll="false"
      :tableRef="tableRef?.getTableRef()"
      @refresh="onSearch"
      @fullscreen="onFullscreen"
    >
      <!-- <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog()"
        >
          新增权限
        </el-button>
      </template> -->
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          adaptive
          :adaptiveConfig="{ offsetBottom: 45 }"
          align-whole="center"
          row-key="id"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading2"
          :size="size"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="{ ...pagination, size }"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #enabled="{ row }">
            <div>
              <span v-if="row.enabled == '0'">申请中</span>
              <span v-if="row.enabled == '1'">已入职</span>
              <span v-if="row.enabled == '2'">离职</span>
            </div>
          </template>
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog(row)"
            >
              修改
            </el-button>
            <el-popconfirm
              :title="`是否确认删除员工名称为${row.username}的这条数据？`"
              @confirm="handleDelete(row.id)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
    <el-dialog
      v-model="formVisible"
      title="编辑"
      width="65%"
      draggable
      :before-close="closeDialog"
    >
      <el-form
        ref="eidtformRef"
        :inline="true"
        :model="editform"
        label-width="120px"
        class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
      >
        <el-form-item label="员工姓名：" prop="username">
          <el-input
            v-model="editform.username"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工账号：" prop="userCode">
          <el-input
            v-model="editform.userCode"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="归属机构：" prop="comName">
          <el-input
            v-model="editform.comName"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="岗位：" prop="gradeName">
          <el-input
            v-model="editform.gradeName"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工状态：" prop="validStatus">
          <el-input v-model="editform.validStatus" disabled class="!w-[360px]">
            <template #input>
              <div>
                <span v-if="editform.validStatus == '0'">申请中</span>
                <span v-if="editform.validStatus == '1'">已入职</span>
                <span v-if="editform.validStatus == '2'">离职</span>
              </div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="客户标识：" prop="customerFlag">
          <el-input
            v-model="editform.customerFlag"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="入职时间：" prop="entryTime">
          <el-input
            v-model="editform.entryTime"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系方式：" prop="phone">
          <el-input
            v-model="editform.phone"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱：" prop="email">
          <el-input
            v-model="editform.email"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <br />
        <el-form-item label="允许机构：" prop="permitCompanyName">
          <el-input
            v-model="editform.permitCompanyName"
            placeholder="请选择允许机构"
            clearable
            disabled
            class="!w-[300px]"
          />
          <el-button type="primary" @click="selectHandle">选择</el-button>
        </el-form-item>
        <el-form-item label="是否除外：" prop="companyFlag">
          <el-select
            v-model="editform.companyFlag"
            placeholder="请选择"
            clearable
            class="!w-[360px]"
          >
            <el-option label="是" value="1" />
            <el-option label="否" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="允许产品：" prop="permitProductName">
          <el-input
            v-model="editform.permitProductName"
            placeholder="请选择允许产品"
            clearable
            disabled
            class="!w-[300px]"
          />
          <el-button type="primary" @click="selectHandle2">选择</el-button>
        </el-form-item>

        <el-form-item label="是否除外：" prop="productFlag">
          <el-select
            v-model="editform.productFlag"
            placeholder="请选择"
            clearable
            class="!w-[360px]"
          >
            <el-option label="是" value="1" />
            <el-option label="否" value="0" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="saveHandle"> 保存 </el-button>
        <el-button @click="closeDialog"> 取消 </el-button>
      </template>
    </el-dialog>
    <!-- 机构 -->
    <organTree ref="treeInfoRef" @cropper="cropper"></organTree>
    <!-- 产品 -->
    <productTree ref="treeInfoRef2" @cropper="cropper2"></productTree>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-table__inner-wrapper::before) {
  height: 0;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
